import { defineStore } from 'pinia'

export const useSetting = defineStore('setting', {
  state: () => ({
    isOK: false, // 数据是否获取到了
    pageSetting: {
      "backgroundImage": '', // 用户当前用的壁纸链接
      "diyBackgroundImage": '', // 用户自定义的壁纸链接
    },
    noteSetting: {
      "noteView": "mt",
      "noteTags": [],
      "calendarTags": [],
      "diyLocations": [], // 用户自定义的坐标
    },
  }),
  actions: {
    init() {
      // 如果用户信息拿到了，里面有setting直接就拿过来
      let setting = User.data.userSetting
      this.pageSetting = { ...this.pageSetting, ...setting.pageSetting }
      this.noteSetting = { ...this.noteSetting, ...setting.noteSetting }

      this.isOK = true
      return this
    },
    // 从服务器获取
    getSetting(fun) {
      myPost('/user/getSetting', { usertoken: User.token }, setting => fun(setting))
    },
    // 更新全部设置
    setSetting(fun) {
      console.log(JSON.stringify(this.$state))
      myPost('/user/set/setting', { usertoken: User.token, value: JSON.stringify(this.$state) }, data => {
        fun && fun(data)
      })
    },
  },
})